package com.example.administrator.mypupowinds.view;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.administrator.mypupowinds.R;

/**
 * Created by Administrator on 2017/10/12.
 */

public class MyPopuWindow extends PopupWindow implements View.OnClickListener {

    private Context context;

    private OnSureClickListener onSureClickListener = null;
    private final TextView surebtn;
    private final TextView cancelBnt;

    private String data;

    public MyPopuWindow(Context context){
       super(context);
       this.context=context;
       View view = View.inflate(context, R.layout.pupowinds,null);
       //LinearLayout  linearLayout =view.findViewById(R.id.parent);
       //设置内容布局
       this.setContentView(view);
       //设置弹出窗体的宽
       this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
       //设置弹出窗体的高
       this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
       //设置弹出框可点击
       this.setFocusable(true);
       //设置弹出窗体动画效果
      // this.setAnimationStyle();
       //实例化一个ColorDrawable颜色为半透明
       ColorDrawable dw = new ColorDrawable(0xb0000000);
       //设置弹出框背景
       this.setBackgroundDrawable(dw);

        surebtn = (TextView) view.findViewById(R.id.sure);
        cancelBnt = (TextView) view.findViewById(R.id.cancle);

        surebtn.setOnClickListener(this);
        cancelBnt.setOnClickListener(this);


   }
    @Override
    public void onClick(View v) {
        if (v==surebtn){
           if (onSureClickListener!=null){
               onSureClickListener.onClick(data);
           }
        }else if (v==cancelBnt){

        }

        dismiss();

    }


    /*
    *
    * 接口回调,数据处理(你喜欢什么类型的就放什么类型就行了
    * )
    * */

    public interface OnSureClickListener{
        public void onClick(String data);
    }

    /*
    * 监听接口函数
    *
    * */

    public void setSureListener(OnSureClickListener onSureClickListener) {
        this.onSureClickListener = onSureClickListener;
    }

    /*
    *
    *
    * 7.0.系统必须重写这个方法，不然pupo位置会确定不了，出现撑高的现象
    *
    * */

    @Override
    public void showAsDropDown(View anchor) {
        if (Build.VERSION.SDK_INT>=24){
            Rect visibleFrame = new Rect();
            anchor.getGlobalVisibleRect(visibleFrame);
            int height = anchor.getResources().getDisplayMetrics().heightPixels-visibleFrame.bottom;
            setHeight(height);
        }
        super.showAsDropDown(anchor);
    }
}
